<?php
use Fox\Common\Customer;
use Fox\Html\Input;
use Fox\Html\TextArea;
use Fox\Common\Customer\Type;
use Doctrine\DBAL\Connection;
use Fox\Generator;
use Fox\MaterialManagement\Material\Hierarchy;
use Fox\Html\DropList;
use Fox\Html\Screen;
use Fox\MaterialManagement\Material;

$xview = Xview::instance();
$id = Xview::instance()->val("customerid");
$type = Xview::instance()->val("type");
$mode = Xview::instance()->val("mode");
$excluded = array();

$searchHelp = new Fox\Html\SearchHelp("Partners");
$searchHelp->setMultiple(new Fox\Html\Button('select',"ReloadFromSelectLines('#b_GRPartners','MC_gPartners','?mod=customer&act=partners','CloseWin(\'sh:Partners\',1)','InputSerialize(\'eCustomer\')');"));
$searchHelp->switchToObjectMode();
$ilink = $xview->currentURL();

$searchHelp->getSelBlock()->add(new TextArea("excluded"))
							->excluded()
							->hidden();
$searchHelp->getSelBlock()->add(new Fox\Html\Input("customerid",false,$id))
							->excluded()
							->hidden();
// Load already selected Product
$blItems = Xview::instance()->component("customer", "partners"); 
$data = $blItems->item("gPartners")->get();
if(is_array($data))
{
	foreach($data as $line)
	{
		if(isset($line['partnerid']))
			$Ids[] = $line['partnerid'];
	}
	if(!empty($id))
		$Ids[] = $id;
	if(isset($Ids))
	{
		$excluded = new \Fox\Dao\SqlFragment("customerid NOT IN (?) ",array($Ids),array(Connection::PARAM_STR_ARRAY));
		$searchHelp->getSelBlock()->item("excluded")->default = addslashes(json_encode($Ids));
	}
}
else
{
	$values = Xview::instance()->val("excluded");
	if(!empty($values))
	{
		$Ids = json_decode(stripcslashes($values),true);
		$excluded = new \Fox\Dao\SqlFragment("customerid NOT IN (?) ",array($Ids),array(Connection::PARAM_STR_ARRAY));
	}
}
$finder = new Type();
$types = $finder->find();
$searchHelp->getSelBlock()->item("customer_type")
	->options($types,Generator::useAttribute("type"),Generator::useAttribute("label"));
$searchHelp->getSelBlock()->item("customer_type")->id("type");

// Filter By Customer Type 
if(!empty($mode)) {
	switch($mode) {
		case 'entity':
			$searchHelp->getSelBlock()->item("customer_type")->initialize('pers');
			$searchHelp->getSelBlock()->item("customer_type")->hide();
			$searchHelp->grid()->getFieldcat()->field("customer_type")->noOutput();
			break;
		case 'employee':
			break;
	}
}


if(isset($_POST['dbcnt']) && 1 == 1)
{
	$inputs = $searchHelp->screen()->get();
	$fragments[] = $searchHelp->screen()->fragments();
	$finder = new Customer();
	if(isset($excluded))
		$fragments[] = $excluded;

	$res = $finder->find(Fox\Dao\SqlFragment::combine($fragments),array("name1" => false),null,$inputs['dbcnt']);
	$searchHelp->grid()->getFieldcat()->field("customer_type")->id("type");	
	$searchHelp->grid()->setDatas($res);
	echo $searchHelp->grid()->Display();
}
else
{
	echo $searchHelp->display();
}
